"""
开放商户 - 商户信息变更相关模型
"""

from typing import Dict, Any
from jlpay.model.base import OrgBaseReq, BaseResponse
from jlpay.model.openmerch.change.merch_info_dto import (
    MerchBaseInfoDto, MerchBusinessInfoDto, 
    MerchIdentificationInfoDto, MerchSettleInfoDto
)
from jlpay.utils.annotation_utils import Encrypt, SensitiveField


@Encrypt  # 标记整个类需要加密处理，对应Java代码中的@DataCrypto(encrypt = true)
class MerchInfoChangeRequest(OrgBaseReq):
    """商户信息变更请求"""
    
    API_URL = "/open/merch/access/merch-info/modify"
    
    def __init__(self):
        """初始化商户信息变更请求"""
        super().__init__()
        self.base_info = None
        self.business_info = None
        self.settle_info = None
        self.identification_info = None
        
        # 定义敏感字段路径（根据Java代码中的@Encrypt注解）
        self._sensitive_fields = [
            # 商户身份信息中的敏感字段
            SensitiveField("identification_info.id_card_name"),           # 经营者/法人姓名
            SensitiveField("identification_info.id_card_english_name"),   # 经营者/法人英文名称
            SensitiveField("identification_info.id_card_no"),             # 经营者/法人身份证件号码
            SensitiveField("identification_info.legal_person_phone"),     # 经营者/法人手机号码
        ]
    
    def get_sensitive_fields(self):
        """
        返回需要加密的敏感字段路径列表
        
        Returns:
            包含敏感字段路径的列表
        """
        return self._sensitive_fields
    
    def set_base_info(self, base_info):
        """
        设置商户基础信息
        
        Args:
            base_info: 商户基础信息DTO
            
        Returns:
            MerchInfoChangeRequest实例
        """
        self.base_info = base_info
        return self
    
    def set_business_info(self, business_info):
        """
        设置商户业务信息
        
        Args:
            business_info: 商户业务信息DTO
            
        Returns:
            MerchInfoChangeRequest实例
        """
        self.business_info = business_info
        return self
    
    def set_settle_info(self, settle_info):
        """
        设置结算信息
        
        Args:
            settle_info: 结算信息DTO
            
        Returns:
            MerchInfoChangeRequest实例
        """
        self.settle_info = settle_info
        return self
    
    def set_identification_info(self, identification_info):
        """
        设置商户身份信息
        
        Args:
            identification_info: 商户身份信息DTO
            
        Returns:
            MerchInfoChangeRequest实例
        """
        self.identification_info = identification_info
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """
        将请求对象转换为字典，处理DTO对象和字典的序列化
        
        Returns:
            字典对象
        """
        data = super().to_dict()
        
        # 处理DTO对象和字典的序列化
        if self.base_info:
            if hasattr(self.base_info, 'to_dict'):
                data['base_info'] = self.base_info.to_dict()
            else:
                data['base_info'] = self.base_info
        
        if self.business_info:
            if hasattr(self.business_info, 'to_dict'):
                data['business_info'] = self.business_info.to_dict()
            else:
                data['business_info'] = self.business_info
        
        if self.settle_info:
            if hasattr(self.settle_info, 'to_dict'):
                data['settle_info'] = self.settle_info.to_dict()
            else:
                data['settle_info'] = self.settle_info
        
        if self.identification_info:
            if hasattr(self.identification_info, 'to_dict'):
                data['identification_info'] = self.identification_info.to_dict()
            else:
                data['identification_info'] = self.identification_info
        
        return data


class MerchInfoChangeResponse(BaseResponse):
    """商户信息变更响应"""
    
    def __init__(self, response_dict=None):
        """初始化商户信息变更响应"""
        super().__init__(response_dict)
        self.merch_no = None
        self.apply_id = None
        
        if response_dict:
            self.merch_no = response_dict.get('merch_no')
            self.apply_id = response_dict.get('apply_id') 